package com.changhai.diabetes.action;

import com.changhai.diabetes.model.OpertResultMsg;
import com.changhai.diabetes.model.Patient;
import com.changhai.diabetes.model.Personal;
import com.changhai.diabetes.service.PersonalService;

/**
 * 个人史
 * 
 * @author Andy
 * 
 */
public class PersonalAction extends BaseAction {

	private static final long serialVersionUID = 4392936553263004630L;
	private OpertResultMsg opertMsg;
	private Long patientid;
	private Patient patient;
	private Personal personal;
	private PersonalService personalService;
	private Integer flag = 0;

	public String execute() throws Exception {
		return super.execute();
	}

	public String query() {
		return "input";
	}

	public String editPersonal() {
		if (patientid != null && patientid > 0) {
			try {
				personal = personalService.searchByPatientId(patientid);
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无个人史信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditPersonal() {
		if (patientid == null || patientid <= 0 || personal == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				personalService.saveEditPersonal(personal);
				this.setFlag(1);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "个人史", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editPersonal();
	}

	public String saveNewPersonal() {
		if (patientid == null || patientid <= 0 || personal == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				personalService.saveNewPersonal(personal);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "个人史", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editPersonal();
	}

	public OpertResultMsg getOpertMsg() {
		return opertMsg;
	}

	public void setOpertMsg(OpertResultMsg opertMsg) {
		this.opertMsg = opertMsg;
	}

	public Long getPatientid() {
		return patientid;
	}

	public void setPatientid(Long patientid) {
		this.patientid = patientid;
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	public Personal getPersonal() {
		return personal;
	}

	public void setPersonal(Personal personal) {
		this.personal = personal;
	}

	public PersonalService getPersonalService() {
		return personalService;
	}

	public void setPersonalService(PersonalService personalService) {
		this.personalService = personalService;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}
}
